const TagModel = require('@/models/tag.model')
const JsonResult = require('@/utils/httpResponse.unit')
const { getSessionuserId } = require('@/utils/common.util')

class TagControllers {
  /**
   * find tags
   * @param {*} req
   * @param {*} response
   */
  async findTags (req, response) {
    try {
      const userId = req.query.userId
      const sessionuserId = await getSessionuserId(req)
      if (!userId && !sessionuserId) {
        return JsonResult.fail({ req, response, message: 'userId 不能为空' })
      }
      const data = await TagModel.find(userId || sessionuserId)
      JsonResult.success({
        req,
        data,
        response,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  /**
   * user add tag
   * @param {*} req
   * @param {*} response
   */
  async additionTag (req, response) {
    try {
      const { pid, tagName } = req.body
      const sessionuserId = req.sessionuser.id
      const tag = await TagModel.findOne({ userId: sessionuserId, tagName })
      if (tag) {
        return JsonResult.fail({ req, response, message: '标签名称已存在' })
      }
      await TagModel.add({ pid, tagName, userId: sessionuserId })
      JsonResult.success({
        req,
        response,
        message: '新增成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '新增失败' })
    }
  }

  /**
   * user delete tag
   * @param {*} req
   * @param {*} response
   * @returns
   */
  async deleteTag (req, response) {
    try {
      const id = req.query.id
      const sessionuserId = req.sessionuser.id
      const tag = await TagModel.findOne({ id })
      if (!tag) {
        return JsonResult.fail({ req, response, message: '标签不存在' })
      }
      if (tag.userId !== sessionuserId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to delete tag',
          code: '9999'
        })
      }
      await TagModel.delete(id)
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }

  /**
   * user update tag
   * @param {*} req
   * @param {*} response
   */
  async updateTag (req, response) {
    try {
      const { id, tagName } = req.body
      const sessionuserId = req.sessionuser.id
      const tag = await TagModel.findOne({ id })
      if (!tag) {
        return JsonResult.fail({ req, response, message: '标签不存在' })
      }
      if (tag.userId !== sessionuserId) {
        return JsonResult.httpStatus(req, response, 403, {
          message: 'No permission to update tag',
          code: '9999'
        })
      }
      await TagModel.update({ id, tagName })
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }
}

module.exports = new TagControllers()
